import React, { PureComponent } from 'react';
import { PrimaryC, PrimaryGhostC } from '../../../../comps';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { message } from 'antd';
import style from './index.less';
import { connect } from 'dva';
import OrderFormC from './orderFormC';
import { uniqueKey } from '../../../../utils/commonFunc';
class AddOrderC extends PureComponent {
  static defaultProps = {
    portfolioOnChange: () => {},
    productOnChange: () => {},
    tradingcodeOnChange: () => {},
  };
  state = {
    info: undefined, //详情
    type: undefined, //当前指令的类型type：1-指令新增   2-簿记新增    3-簿记详情
    isShow: false, //二次确认模态框显示隐藏
    querys: {}, //二次确认详情数据
    trailColumns: [], //试算结果列表
    trailDataSource: [], //试算结果数据
    trailModel: false, //试算模态框
    productCode: undefined, //产品code
    portfolioCode: undefined, //组合code
    uid: '',
  };
  componentDidMount() {
    this.props.onRef(this);
    const uid = uniqueKey();
    uid.then((r) => {
      this.setState({ uid: r });
    });
  }
  //用户点击下达或确定按钮
  onOk = () => {
    const data = this.orderForm.getInfo();
    //校验页面数据
    if (!data) return;
    data.uid = this.state.uid;
    //簿记无需二次确认 直接请求后端
    this.confirmOk(data);
  };
  //用户点击取消
  onCancel = () => {
    this.orderForm.reset();
  };
  confirmOk = (params) => {
    console.log(params);
    //指令下达 簿记保存请求后台
    const { dispatch, type } = this.props;
    if (type == 2) {
      //簿记下达
      params.method = '0';
    }
    let url = 'stockIndexM/tradeBookSave';
    dispatch({
      type: url,
      payload: {
        ...params,
      },
      callback: (msg) => {
        if (msg) {
          //簿记下达成功
          this.orderForm.reset();
        }
        const uid = uniqueKey();
        uid.then((r) => {
          this.setState({ uid: r });
        });
      },
    });
  };

  //获取组合code
  portfolioCode = (e) => {
    if (e) {
      this.setState({ portfolioCode: e });
    } else this.setState({ portfolioCode: undefined });
  };
  //获取产品code
  productCode = (e) => {
    if (e) {
      this.setState({ productCode: e });
    } else this.setState({ productCode: undefined });
  };
  render() {
    const { loading, type } = this.props;
    return (
      <div>
        <div>
          <OrderFormC
            type={this.props.type}
            portfolioOnChange={(e) => {
              this.props.portfolioOnChange(e);
            }}
            tradingcodeOnChange={this.props.tradingcodeOnChange}
            portfolioCode={this.portfolioCode}
            productCode={this.productCode}
            onRef={(ref) => {
              this.orderForm = ref;
            }}
            money={this.props.money}
            position={this.props.position}
            futuresInfo={this.props.futuresInfo}
            holdData={this.props.holdData}
          />
        </div>
        <div style={{ marginTop: '20px' }} className={style.btnOperate}>
          <PrimaryC loading={loading.effects['stockIndexM/tradeBookSave'] === true} onClick={this.onOk} title={this.props.type == 1 ? '下达' : '确定'} />
          <PrimaryGhostC onClick={this.onCancel} title='取消' style={{ marginLeft: '16px' }} />
        </div>
      </div>
    );
  }
}
export default connect(({ stockIndexM, userM: user, loading, trailM, isAccountConfiguredM }) => ({
  stockIndexM,
  user,
  loading,
  trailM,
  errorSetMsg: isAccountConfiguredM.msg,
  isAccountConfiguredM,
}))(Form.create()(AddOrderC));
